JavaJava文件的拷贝[Java]
Jayfar输入流读取数据,输出流写入数据,在输入的同时,进行输出实现数据的拷贝
- 输入流读取数据,输出流写入数据,在输入的同时,进行输出实现数据的拷贝

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package cn.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
public class Copy {
public static void main(String[] args) throws IOException { File desFile = new File("abc"); File srcFile = new File("ab.txt"); OutputStream oStream = null; InputStream iStream = null; try { oStream = new FileOutputStream(srcFile,true); iStream = new FileInputStream(desFile); byte[] flush = new byte[1024]; int len = -1; while((len = iStream.read(flush)) != -1) { oStream.write(flush,0,len); } oStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (null != oStream) { oStream.close(); } if (package cn.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
public class Copy {
public static void main(String[] args) throws IOException { File desFile = new File("abc"); File srcFile = new File("ab.txt"); OutputStream oStream = null; InputStream iStream = null; try { oStream = new FileOutputStream(srcFile,true); iStream = new FileInputStream(desFile); byte[] flush = new byte[1024]; int len = -1; while((len = iStream.read(flush)) != -1) { oStream.write(flush,0,len); } oStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (null != oStream) { oStream.close(); } if (null != iStream) { iStream.close(); } } }
}
|


最后进行封装,复制任何文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package cn.io;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
public class Copy {
public static void main(String[] args) throws IOException { copy("E:\\JAVA\\cn.shangxuetang\\image\\2.jpg", "2copy.jpg"); }
public static void copy(String srcStream,String destStream) throws IOException { File desFile = new File(srcStream); File srcFile = new File(destStream); OutputStream oStream = null; InputStream iStream = null; try { oStream = new FileOutputStream(srcFile,true); iStream = new FileInputStream(desFile); byte[] flush = new byte[1024]; int len = -1; while((len = iStream.read(flush)) != -1) { oStream.write(flush,0,len); } oStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (null != oStream) { oStream.close(); } if (package cn.io;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
public class Copy {
public static void main(String[] args) throws IOException { copy("E:\\JAVA\\cn.shangxuetang\\image\\2.jpg", "2copy.jpg"); }
public static void copy(String srcStream,String destStream) throws IOException { File desFile = new File(srcStream); File srcFile = new File(destStream); OutputStream oStream = null; InputStream iStream = null; try { oStream = new FileOutputStream(srcFile,true); iStream = new FileInputStream(desFile); byte[] flush = new byte[1024]; int len = -1; while((len = iStream.read(flush)) != -1) { oStream.write(flush,0,len); } oStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if (null != oStream) { oStream.close(); } if (null != iStream) { iStream.close(); } } } }
|